package com.jimmy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.jimmy.reggie.common.CustomException;
import com.jimmy.reggie.common.R;
import com.jimmy.reggie.entity.*;
import com.jimmy.reggie.mapper.DishMapper;
import com.jimmy.reggie.service.DishFlavorService;
import com.jimmy.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */

    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //这里记得表里存的字段名称，当id多了的时候就容易搞混了
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    public void UpdateWithFlavor(DishDto dishDto) {
        //1.先把除了口味以外的信息存储了，然后第二个就是清楚对应id的口味信息，然后重新赋值
        this.updateById(dishDto);
        //2.清除口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        //3. 现在开始存储口味的信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }
    /**
     * 批量删除
     * A. 查询该批次套餐中是否存在售卖中的套餐, 如果存在, 不允许删除
     * B. 删除套餐数据
     * C. 删除套餐关联的菜品数据
     * @param ids
     */
//    @Override
//    @Transactional
//    public void removeWithDish(List<Long> ids) {
//        //select count(*) from setmeal where id in (1,2,3) and status = 1
//        //查询套餐状态，确定是否可用删除
//        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
//        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
//        int count = this.count(setmealLambdaQueryWrapper);
//        if (count>0){
//            throw new CustomException("套餐正在售卖中，无法进行删除操作");
//        }
//        this.removeByIds(ids);
//        //删除关联数据
//        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
//        setmealDishService.remove(setmealDishLambdaQueryWrapper);
//    }
    /**
    * 批量删除
    * A. 查询该批次套餐中是否存在售卖中的套餐, 如果存在, 不允许删除
    * B. 删除套餐数据
     * C. 删除套餐关联的菜品数据
    */
    @Override
    public void removeWithFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        int count=this.count(dishLambdaQueryWrapper);
        if (count>0){
            throw  new CustomException("菜品正在售卖中，请停售后再进行删除");
        }


    }

}
